package com.sky.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.sky.constant.MessageConstant;
import com.sky.constant.StatusConstant;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.entity.Setmeal;
import com.sky.entity.SetmealDish;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealDishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.result.PageResult;
import com.sky.service.SetmealService;
import com.sky.vo.DishItemVO;
import com.sky.vo.SetmealVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class SetmealServiceImpl implements SetmealService {

    @Autowired
    private SetmealMapper setmealMapper;

    @Autowired
    private SetmealDishMapper setmealDishMapper;

    @Autowired
    private DishMapper dishMapper;

    /**
     * 增加套餐
     * @param setmealVO
     */
    @Transactional
    @Override
    public void setmealInsert(SetmealVO setmealVO) {

        Setmeal setmeal = new Setmeal();
        //将相同的数据拷贝到setmeal
        BeanUtils.copyProperties(setmealVO, setmeal);
        setmealMapper.setmealInsert(setmeal);

        //取出setmeal_dish表中的数据添加
        List<SetmealDish> setmealDishes = setmealVO.getSetmealDishes();

        //获取setmeal表的主键值
        Long id = setmeal.getId();

        //判断setmealDishes是否为空
        if (setmealDishes != null && setmealDishes.size() > 0){
            //遍历setmealDishes为SetmealId赋值
            setmealDishes.forEach(setmealDish -> {
                setmealDish.setSetmealId(id);
            });
            //为setmeal——dishes表添加数据
            setmealDishMapper.setmealDishInsert(setmealDishes);
        }

    }

    /**
     * 根据id查询
     * @param id
     * @return
     */
    @Override
    public SetmealVO SetmeaById(Long id) {

        SetmealVO setmealVO = setmealMapper.SetmeaById(id);
        return setmealVO;
    }

    /**
     * 分页查询
     * @param setmealPageQueryDTO
     * @return
     */
    @Override
    public PageResult pageList(SetmealPageQueryDTO setmealPageQueryDTO) {

        PageHelper.startPage(setmealPageQueryDTO.getPage(), setmealPageQueryDTO.getPageSize());

        List<Setmeal> setmealList = setmealMapper.pageList(setmealPageQueryDTO);

        PageInfo<Setmeal> setmealPageInfo = new PageInfo<>(setmealList);

        return new PageResult(setmealPageInfo.getTotal(), setmealPageInfo.getList());
    }

    /**
     * 修改数据
     * @param setmealVO
     * @return
     */
    @Transactional
    @Override
    public void setmeaUpdate(SetmealVO setmealVO) {

        Setmeal setmeal = new Setmeal();

        BeanUtils.copyProperties(setmealVO, setmeal);

        setmealMapper.setmeaUpdate(setmeal);

        List<SetmealDish> setmealDishes = setmealVO.getSetmealDishes();

        if (setmealDishes != null && setmealDishes.size() > 0){
            setmealDishes.forEach(setmealDish -> {
                setmealDish.setSetmealId(setmeal.getId());
            });
            setmealDishMapper.setmealDishDelete(setmeal.getId());
            setmealDishMapper.setmealDishInsert(setmealDishes);
        }
    }

    /**
     * 根据id批量删除
     * @param ids
     */
    @Override
    public void setmeaDelete(List<Long> ids) {

        for (Long aLong :ids) {
           Setmeal setmeal =  setmealMapper.query(aLong);

           if (setmeal.getStatus() == StatusConstant.ENABLE){
               throw new DeletionNotAllowedException(MessageConstant.SETMEAL_ON_SALE);
           }
        }


        setmealMapper.setmeaDelete(ids);
        setmealDishMapper.setmeaDelete(ids);
    }

    /**
     *套餐的起售和禁售
     * @param status
     * @param id
     */
    @Override
    public void setmealStatus(Integer status, Long id) {

        //判断status是否是要起售
        if (status == StatusConstant.ENABLE){
            //查询setmeal_dish表中dishId属性
            List<Long> dishIdList = setmealDishMapper.setmealDishId(id);

            //查询dish表中status属性
            List<Integer> integerList =  dishMapper.idList(dishIdList);
            for (Integer integer :integerList) {
                //判断菜品是否停售状态
                if (integer == StatusConstant.DISABLE){
                    //如果是停售状态抛出异常
                    throw new DeletionNotAllowedException(MessageConstant.SETMEAL_ENABLE_FAILED);
                }
            }

            //如果不是则修改表中数据
            setmealMapper.setmealStatusAvailability(id);
        }else if (status == StatusConstant.DISABLE){
            //直接停售套餐
            setmealMapper.setmealStatus(id);
        }
    }

    /**
     * 条件查询
     * @param setmeal
     * @return
     */
    public List<Setmeal> list(Setmeal setmeal) {
        List<Setmeal> list = setmealMapper.list(setmeal);
        return list;
    }

    /**
     * 根据id查询菜品选项
     * @param id
     * @return
     */
    public List<DishItemVO> getDishItemById(Long id) {
        return setmealMapper.getDishItemBySetmealId(id);
    }
}
